package tako.wisers.com.android.apis.http;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.android.maps.GeoPoint;

import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.item.PanoramioItem;
import tako.wisers.com.android.apis.location.GPSManager;
import tako.wisers.com.android.apis.location.Geohash;
import tako.wisers.com.android.apis.util.BitmapUtils;
import tako.wisers.com.android.apis.util.Configration;
import tako.wisers.com.android.apis.util.Global;
import tako.wisers.com.android.apis.util.LocationRange;
import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;

public class PanoramioService {
	private static final String TAG = "Panoramio";
	private Context mContext;
	private final Geohash encoder = new Geohash();

	public PanoramioService(Context context) {
		mContext = context;
	}

	public List<PanoramioItem> getPanotamioItems(GeoPoint point) {
		LocationRange lr = GPSManager.getLocationRange(point);
		String url = Configration.getInstance().getProperty(
				Global.PANORAMIO_SERVICE_KEY);
		url = String.format(url, lr.getMinLat(), lr.getMinLong(), lr
				.getMaxLat(), lr.getMaxLong());
		List<PanoramioItem> items = new ArrayList<PanoramioItem>();
		try {
			HttpGeter get = new HttpGeter(url);
			String str = get.getContent();
			JSONObject json = new JSONObject(str);
			items = parse(json, encoder.encode(point.getLatitudeE6(), point
					.getLongitudeE6(), Geohash.PHOTOBIT));
		} catch (Exception e) {
			Log.e(TAG, e.toString());
		}
		return items;
	}

	private List<PanoramioItem> parse(JSONObject json, String keyword) {
		try {
			List<PanoramioItem> items = new ArrayList<PanoramioItem>();
			JSONArray array = json.getJSONArray("photos");
			int count = array.length();
			for (int i = 0; i < count; i++) {
				JSONObject obj = array.getJSONObject(i);

				long id = obj.getLong("photo_id");
				String title = obj.getString("photo_title");
				String owner = obj.getString("owner_name");
				String thumb = obj.getString("photo_file_url");
				String ownerUrl = obj.getString("owner_url");
				String photoUrl = obj.getString("photo_url");
				double latitude = obj.getDouble("latitude");
				double longitude = obj.getDouble("longitude");
				if (title == null) {
					title = mContext.getString(R.string.untitled);
				}
				ThreadGroup group = BitmapUtils.GROUP_BITMAP;
				while(group.activeCount() >= Global.MAX_BITMAP_LOAD_SIZE){
					Thread.yield();
				}
				PanoramioItem item = new PanoramioItem(id, thumb,
						(int) (latitude * Global.MILLION),
						(int) (longitude * Global.MILLION), title, owner,
						ownerUrl, photoUrl, keyword);
				items.add(item);
			}
			while(!BitmapUtils.bitmapLoadFinish()){
				Thread.yield();
			}
			return items;
		} catch (JSONException e) {
			Log.e(TAG, e.toString());
		}
		return new ArrayList<PanoramioItem>();
	}
}
